/** @file gui_typemgr.h
    @brief Typemanager for the widgets */

#ifndef G3_GUI_TYPEMGR_H
#define G3_GUI_TYPEMGR_H

#include "singleton.h"
#include "types.h"

#include <map>

namespace G3 {

/**
    @class TYPE_MANAGER
    @brief A type manager class.
    With this, the GUI knows to which WIDGET subclasses WIDGET pointers point to.
    It automatically generates an index for every new widget type.
*/
class TYPE_MANAGER: public SINGLETON<TYPE_MANAGER> {
	public:
        /**
            @brief Constructor
        */
		TYPE_MANAGER () { LastTypeID = 0; }

        /**
            @brief Gets a new index for a widget type
            @param[in] aName Reference to the widget name string
            @return The new index
        */
		uint GetType (const std::string &aName) {
			std::pair <std::map<std::string, uint>::iterator, bool> nipair;

			nipair = TypeMap.insert (std::pair <std::string, uint> (aName, GetNewType ()));

			if (!nipair.second) {
				--LastTypeID;
				return nipair.first->second;
			}
			return LastTypeID;
		}

    protected:
        /** Last index for a new type */
		uint LastTypeID;
		/** Maps a widget type string to an index */
		std::map<std::string, uint> TypeMap;

        /**
            @brief Increments the last index
            @return The new index
        */
		uint GetNewType () {
			return ++LastTypeID;
		}
};

/** Main type manager singleton instance */
extern TYPE_MANAGER MainTypeMgr;

}

#endif // G3_GUI_TYPEMGR_H
